<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="axios.js"></script>
    <script src="vue.js"></script>
    <script>
        onload=function () {
            new Vue({
                el:"#d",
                data:{
                    token:localStorage.getItem('token')
                },
                methods:{
                    f:function () {
                        /*$.ajax({
                            url: "http://localhost/user/test",
                            type: "POST",
                            dataType: "json",
                            headers: {
                                token: this.token //这里是Token
                            }
                        });*/
                        axios({
                            method:"post",
                            url:"http://localhost/user/test",
//                            headers:{token:this.token}
                        }).then(ret=> {
                            alert(ret.data.msg);
                        });
                    }
                }
            });
        }
        /*请求拦截器，每次发送请求时，都向头中添加token*/
        axios.interceptors.request.use(
            cfg => {
                cfg.headers.token = localStorage.getItem('token');
                return cfg;
            });
    </script>
</head>
<body>
<div id="d">
    <input  type="button" @click="f" value="测试">
</div>
</body>
</html>